package com.icbc.cc.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSONObject;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;

public class CallApiServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private static Log log = LogFactory.getCtpLog(CallApiServlet.class);

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String apiName = request.getParameter("opName");
		log.info("ApiCommonUtils.excuteOp opName:" + apiName);
		String param = request.getParameter("param");
		log.info("ApiCommonUtils.excuteOp param:" + param);
		String osFlag = request.getParameter("osFlag");
		String versionNo = request.getParameter("versionNo");
		//String userId = request.getParameter("userId");
		String result = null;//!"".equals(apiName)

		if(!"order_acce_upload_IOS".equals(apiName)&&!"get_customer_typeList".equals(apiName)&&!"5".equals(osFlag)&&!"check_phone_exists".equals(apiName)&&!"check_phone_checkCode".equals(apiName)&&!"user_regist".equals(apiName)&&!"user_login".equals(apiName)&&!"reset_password".equals(apiName)&&!"get_ads_info".equals(apiName)&&!"update_password".equals(apiName)&&!"check_version".equals(apiName)&&!"get_product_StatusList".equals(apiName)&&!"save_tailor_info".equals(apiName)){
			result = CallApi.callCheckVersionUsableApi(osFlag,versionNo,param);
			JSONObject newobj=JSONObject.parseObject(result);
			if("9999".equals(newobj.getString("retCode"))){
				result = ApiCommonUtils.encrypAndSign(result, osFlag);
				out.write(result);
				out.flush();
				out.close();
				return;
			}
		}
		
//		result = CallApi.callCheckVersionUsableApi(osFlag,versionNo,param);
//		JSONObject leaveobj=JSONObject.parseObject(result);
//		if("7777".equals(leaveobj.getString("retCode"))){
//			result = ApiCommonUtils.encrypAndSign(result, osFlag);
//			out.write(result);
//			out.flush();
//			out.close();
//			return;
//		}
		
		InputStream file=null;
		if("order_acce_upload_IOS".equals(apiName)){
			DiskFileItemFactory factory=new DiskFileItemFactory();
	        ServletFileUpload sfu=new ServletFileUpload(factory);
	        sfu.setHeaderEncoding("UTF-8");  //处理中文问题
	        //sfu.setSizeMax(1024*1024);   //限制文件大小
	        String path=request.getRealPath("/file");
	        List piclist=new ArrayList();
	        
	        try {
	            List<FileItem> fileItems= sfu.parseRequest(request);  //解码请求 得到所有表单元素
	            for (FileItem fi : fileItems) {
	                //有可能是 文件，也可能是普通文字 
	                if (fi.isFormField()) { //这个选项是 文字 
	                	String key = fi.getFieldName();
	                	String field = fi.getString();
	            		if("param".equals(key)){
	            			param=field;
	            		}else if("osFlag".equals(key)){
	            			osFlag=field;
	            		}
	            			                	
	                }else{
	                	
	                	file=fi.getInputStream();
	                	
	                	
	                    // 是文件
//	                    String fn=fi.getName();
//	                    System.out.println("文件名是："+fn);  //文件名 
//	                    // fn 是可能是这样的 c:\abc\de\tt\fish.jpg
//	                    fi.write(new File(path,fn));
//	                    
//	                    if (fn.endsWith(".jpg")) {
//	                        piclist.add(fn);  //把图片放入集合
//	                    }
	                }                
	            }    
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		}
		
		
		
		// 校验数字签名、解密
//		param = AppCommonUtils.decrypAndSign(param, AppCommonUtils.APP_CALL_FLAG, osFlag);
		
		log.info("CallApiServlet param:" + param);
		
		
		//用户登录
		if ("user_login".equals(apiName)) {
			result = CallApi.callUserLoginApi(param,osFlag);
		}
		//获取首页图片列表
		else if ("get_ads_info".equals(apiName)) {
			result = CallApi.callGetadsApi(param,osFlag);
		}
		//校验验证码
		else if ("check_phone_checkCode".equals(apiName)) {
			result = CallApi.callCheckCodeApi(param,osFlag);
		}
		//用户注册
		else if ("user_regist".equals(apiName)) {
			result = CallApi.callUserRegistApi(param,osFlag);
		}
		//用户忘记密码
		else if ("reset_password".equals(apiName)) {
			result = CallApi.callUserResetPasswordApi(param,osFlag);
		}
		
		//获取个人信息
		else if ("get_userInfo".equals(apiName)) {
			result = CallApi.callGetUserInfoApi(param,osFlag);
		}
		
		//校验手机号是否存在
		else if ("check_phone_exists".equals(apiName)) {
			result = CallApi.callCheckPhoneApi(param,osFlag);
		}
		
		//------------------------------手势密码-----------------------------
		
		//验证密码
		else if ("validate_password".equals(apiName)) {
			result = CallApi.callValidatePasswordApi(param,osFlag);
		}
		
		//设置手势密码
		else if ("update_sign_password".equals(apiName)) {
			result = CallApi.callUpdateSignPasswordApi(param,osFlag);
		}
		
		//手势密码登录
		else if ("user_sign_login".equals(apiName)) {
			result = CallApi.callUserSignLoginApi(param,osFlag);
		}
		//--------------------------------账户-------------------------------
		//修改密码
		else if ("update_password".equals(apiName)) {
			result = CallApi.callUpdatePasswordApi(param,osFlag);
		}
		
		//版本更新
		else if ("check_version".equals(apiName)) {
			result = CallApi.callCheckVersionApi(param,osFlag);
		}
		
		//关于我们
		else if ("check_describe".equals(apiName)) {
			result = CallApi.callCheckDescribeApi(param,osFlag);
		}
		
		//-------------------------------订单--------------------------------
		else if ("delete_subscribe_order".equals(apiName)) {
			result = CallApi.deleteRegistStatus(param,osFlag);
		}
		else if ("delete_my_order".equals(apiName)) {
			result = CallApi.callDeleteOrderApi(param,osFlag);
		}
		// 查询订单列表
		else if ("query_my_orderList".equals(apiName)) {
			result = CallApi.callGetMyOrderListApi(param,osFlag);
		}
		// 获取订单详情
		else if ("query_order_detail".equals(apiName)) {
			result = CallApi.callGetOrdertDetailApi(param,osFlag);
		}
		// 查询客户经理的客户列表
		else if ("customer_query_custlist".equals(apiName)) {
			result = CallApi.callGetCustListApi(param,osFlag,apiName);
		}
		// 根据产品名称或产品编码模糊查询产品列表接口
		else if ("product_fuzyy_querylist".equals(apiName)) {
			try {
				result = CallApi.callFuzyyProductListApi(param,osFlag,apiName);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// 根据登记信息查询当天是否有相似订单接口
		else if ("order_register_similar".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,"order_register_query_similar");
		}
		// 订单登记接口
		else if ("order_register".equals(apiName)) {
			result = CallApi.callOrderRegisterApi(param,osFlag,apiName);
		}
		
		// 修改已登记与退回状态订单接口
		else if ("order_register_update".equals(apiName)) {
			result = CallApi.callOrderUpdateApi(param,osFlag,apiName);
		}
		
		// 获取订单状态接口
		else if ("order_status_query".equals(apiName)) {
			result = CallApi.callGetOrdertStatusApi(param,osFlag);
		}
		
		// 划款确认获取客户账户列表接口
		else if ("order_transfer_query_custcountlist".equals(apiName)) {
			result = CallApi.callGetCustCountListApi(param,osFlag,apiName);
		}
		
		// 客户账户ID存在时获取客户账户详细信息接口
		else if ("order_custaccountid_detail".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,apiName);
		}
		
		//划款确认获取总行名称列表接口
		else if ("order_query_hqbank".equals(apiName)) {
			result = CallApi.callGetBankListApi(param,osFlag,apiName);
		}
		
		//划款确认根据银行总行获取分行列表接口
		else if ("order_query_branchbank".equals(apiName)) {
			result = CallApi.callGetBankListApi(param,osFlag,apiName);
		}
		
		//划款确认获取证件类型列表
		else if ("order_transfer_query_certtypelist".equals(apiName)) {
			result = CallApi.callGetBankListApi(param,osFlag,apiName);
		}
		
		//划款确认时获取客户性质列表接口
		else if ("order_transfer_query_custproplist".equals(apiName)) {
			result = CallApi.callGetBankListApi(param,osFlag,apiName);
		}
		
		//划款确认时获取销售类型列表接口
		else if ("order_transfer_query_saletypelist".equals(apiName)) {
			result = CallApi.callGetBankListApi(param,osFlag,apiName);
		}
		
		//划款信息更改获取划款账户信息
		else if ("orderupdate_custaccountid_detail".equals(apiName)) {
			result = CallApi.callGetOrderAccountUpateDetailApi(param,osFlag);
		}
		
		//划款确认提交接口
		else if ("order_transfer_confirm".equals(apiName)) {
			result = CallApi.callOrderTransferApi(param,osFlag,apiName);
		}
		
		//上传附件（划款凭条&合同）
		else if ("order_acce_upload".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,apiName);
		}
		
		//上传附件（划款凭条&合同）(IOS专用)
		else if ("order_acce_upload_IOS".equals(apiName)) {
			result = CallApi.callExcuteOpFileApi(param,osFlag,apiName,file);
		}
		
		//订单模块上传合同时模糊查询经办人列表接口
		else if ("order_acce_query_agentlist".equals(apiName)) {
			result = CallApi.callGetAgentListApi(param,osFlag);
		}
		
		//修改回访驳回订单重新提交接口
		else if ("order_retvisit_retryvisit".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,apiName);
		}

		
		 //获取合同照片上传类型
		else if ("get_pact_typelist".equals(apiName)) {
			result = CallApi.callGetPactTypeListApi(param,osFlag);
		}
		
		 //重新上传合同文件之前删除原有合同文件接口
		else if ("order_acce_delete".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,apiName);
		}
		
		 //上传合同最后提交接口
		else if ("order_acce_cop_end".equals(apiName)) {
			result = CallApi.callExcuteOpApi(param,osFlag,apiName);
		}
		
		// 获取凭条照片列表
		else if ("get_proof_piclist".equals(apiName)) {
			result = CallApi.callGetProofPicListApi(param,osFlag);
		}
		//------------------------------------------产品-----------------------------------
		// 获取产品列表
		else if ("get_product_StatusList".equals(apiName)) {
			result = CallApi.callGetMyProductListApi(param,osFlag,apiName);
		}
		//非标产品预约
		else if ("prod_subscribe".equals(apiName)) {
			result = CallApi.callOrderRegisterApi(param,osFlag);
		}
		
		// 获取最新上架5支产品
		else if ("get_new_productList".equals(apiName)) {
			result = CallApi.callGetNewProductListApi(param,osFlag,apiName);
		}
		// 模糊查询产品列表
		else if ("get_blur_productList".equals(apiName)) {
			result = CallApi.callGetMyBlurProductListApi(param,osFlag,apiName);
		}
		//获取私人定制客户类型列表
		else if ("get_customer_typeList".equals(apiName)) {
			result = CallApi.callGetTailorTermListpi(param,osFlag);
		}
		//存储私人订制信息
		else if ("save_tailor_info".equals(apiName)) {
			result = CallApi.callSaveTailorApi(param,osFlag);
		}
		//获取不带产品类型的产品列表
		else if ("get_product_List".equals(apiName)) {
			result = CallApi.callGetMyProductListApi(param,osFlag,apiName);
		}
		
		// 查询产品详细信息和收益方案列表
		else if ("get_product_Detail".equals(apiName)) {
			result = CallApi.callGetProductDetailApi(param,osFlag);
		}
		
		// 下载产品培训资料和营销工具
		else if ("get_product_Tools".equals(apiName)) {
			result = CallApi.callGetProductFileListApi(param,osFlag);
		}
		
		//获取产品培训资料和营销工具附件
		else if ("get_product_train".equals(apiName)) {
			result = CallApi.callGetProductTrainListApi(param,osFlag);
		}
		// 获取产品发行方案
		else if ("get_product_issueplan".equals(apiName)) {
			result = CallApi.callGetProductIssueplanApi(param,osFlag);
		}
		
		// 获取产品募集账户
		else if ("get_product_accountinfo".equals(apiName)) {
			result = CallApi.callGetProductAccountInfoApi(param,osFlag);
		}
		
		// 获取产品运营要求
		else if ("get_product_omask".equals(apiName)) {
			result = CallApi.callGetProductOmAskApi(param,osFlag);
		}
		
		// 获取产品合同
		else if ("get_product_pact".equals(apiName)) {
			result = CallApi.callGetProductPactListApi(param,osFlag);
		}
		
		// 获取推荐材料
		else if ("get_product_groom".equals(apiName)) {
			result = CallApi.callGetProductGroomListApi(param,osFlag);
		}
		
		// 产品废弃时删除该产品附件
		else if ("product_file_delete".equals(apiName)) {
			result = CallApi.callProductFileDeleteApi(param,osFlag);
		}
		
		// 删除所有缓存附件
		else if ("product_allfile_delete".equals(apiName)) {
			result = CallApi.callAllProductFileDeleteApi();
		}
		
//		// 在投产品提醒
//		else if ("get_cast_productList".equals(apiName)) {
//			result = CallApi.callGetCastProductListPushApi();
//		}
		//---------------------------------------------------提醒-----------------------------------
		// 查询在投产品提醒列表
		else if ("get_paymentOrder_List".equals(apiName)) {
			result = CallApi.callGetMyPaymenttOrderListApi(param,osFlag);
		}
		
		//根据财富师id获取规定时间内过生日的客户
		else if ("get_birthCust_list".equals(apiName)) {
			try {
				result = CallApi.callGetCustBirthListApi(param,osFlag);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
		//订单变更推送提醒消息
		else if ("push_user_message".equals(apiName)) {
			result = CallApi.callUserPushApi(param,osFlag);
		}
		
		//获取订单状态变更列表
		else if ("get_orderupdate_list".equals(apiName)) {
			result = CallApi.callGetMyOrderUpdateListApi(param,osFlag);
		}
		
		//------------------------------客户---------------------------------------
		
		//查询累计服务、本月新增客户数资产规模
		else if ("get_customer_asesamnt".equals(apiName)) {
			result = CallApi.callGetMyCustomerAsesamntApi(param,osFlag);
		}
		
		//获取财富师客户列表
		else if ("get_customer_list".equals(apiName)) {
			result = CallApi.callGetMyCustomerListApi(param,osFlag);
		}
		
		//获取客户信息
		else if ("get_customer_info".equals(apiName)) {
			result = CallApi.callGetCustomerInfoApi(param,osFlag);
		}
		
		//获取客户订单列表
		else if ("get_customerorder_list".equals(apiName)) {
			result = CallApi.callGetMyCustomerOrderListApi(param,osFlag);
		}
		
		//获取客户个人详细信息
//		else if ("get_customer_detail".equals(apiName)) {
//			result = CallApi.callGetCustomerDetailApi(param,osFlag);
//		}
		
		//------------------------------公告----------------------------------------
		//获取公告更新信息
		else if ("get_notice_updatelist".equals(apiName)) {
			result = CallApi.callGetNoticeUpdateListApi(param,osFlag);
		}
		
		//获取公告类别更新信息
		else if ("get_noticetype_updatelist".equals(apiName)) {
			result = CallApi.callGetNoticeTypeUpdateListApi(param,osFlag);
		}
		
		//获取公告项信息
		else if ("get_noticeoption_list".equals(apiName)) {
			result = CallApi.callGetNoticeOptionListApi(param,osFlag);
		}
		//获取消息类别列表
		else if ("get_opnews_type".equals(apiName)) {
			result = CallApi.callGetOpnewsTypeListApi(param,osFlag);
		}
		//获取公告附件
		else if ("get_noticeoption_filelist".equals(apiName)) {
			result = CallApi.callGetNoticeFileListApi(param,osFlag);
		}

		//获取公告详细信息
		else if ("get_noticedetail".equals(apiName)) {
			result = CallApi.callGetNoticeDetailApi(param,osFlag);
		}
		
		//获取业绩播报(当日登记订单)
		else if ("get_regorder_list".equals(apiName)) {
			result = CallApi.callGetRegOrderListpi(param,osFlag);
		}
		
		//-------------------------------家-----------------------------------------
		//获取展业资料链接列表
		else if ("get_homedata_list".equals(apiName)) {
			result = CallApi.callGetHomeDataListApi(param,osFlag);
		}
		
		//获取研究资讯类别列表
		else if ("get_homeoption_typelist".equals(apiName)) {
			result = CallApi.callGetHomeOptionTypeListApi(param,osFlag);
		}
		
		//获取研究资讯链接列表
		else if ("get_homeoption_list".equals(apiName)) {
			result = CallApi.callGetHomeoptionListApi(param,osFlag);
		}
		
		//存储留言
		else if ("insert_advise".equals(apiName)) {
			result = CallApi.callInsertAdviseApi(param,osFlag);
		}
		//获取客服电话
		else if ("get_service_phone".equals(apiName)) {
			result = CallApi.callGetServicePhoneApi(param,osFlag);
		}
		
		//插入崩溃信息
		else if ("insert_exception_info".equals(apiName)) {
			result = CallApi.callInsertExceptionApi(param,osFlag);
		}
		
		//------------------------------------------面签------------------------------------------------------
		
		//查询预约时间与坐席数量接口
		else if ("query_apt_list".equals(apiName)) {
			result = CallApi.callQueryAptListApi(param,osFlag);
		}
		//预约接口
		else if ("save_apt_info".equals(apiName)) {
			result = CallApi.callSaveAptListApi(param,osFlag);
		}
		
		//修改预约接口
		else if ("edit_apt_info".equals(apiName)) {
			result = CallApi.callEditAptListApi(param,osFlag);
		}
		
		//取消预约接口
		else if ("cancel_apt_info".equals(apiName)) {
			result = CallApi.callCancelAptListApi(param,osFlag);
		}
		
		//查询视频面签预约状态时间等信息接口
		else if ("order_interview_info".equals(apiName)) {
			result = CallApi.callGetInterViewInfoApi(param,osFlag);
		}
		
		//查询视频认证预约状态时间等信息接口
		else if ("person_interview_info".equals(apiName)) {
			result = CallApi.callGetPersonInterViewInfoApi(param,osFlag);
		}
		
		//修改视频接通状态接口
		else if ("edit_apt_status".equals(apiName)) {
			result = CallApi.callEditAptStatusApi(param,osFlag);
		}
		//---------------------------------------------客户订单详情--------------------------------------------
		// 七日年化收益率列表接口
		else if ("private_profit_list".equals(apiName)) {
			result = CallApi.callPrivateProfitListApi(param,osFlag);
		}
		
		// 私募资产购买详情接口
		else if ("private_trade_detail".equals(apiName)) {
			result = CallApi.callPrivateTradeDetailApi(param,osFlag);
		}
		
		//-----------------------------------------------KPI考核-----------------------------------------------
		
		// 获取财富师kpi考核信息
		else if ("get_person_kpi".equals(apiName)) {
			result = CallApi.callGetPersonKpiApi(param,osFlag);
		}
		
		// 获取财富师相关会员
		else if ("get_member_list".equals(apiName)) {
			result = CallApi.callgetMenberListApi(param,osFlag);
		}
		
		// 获取财富管理部总经理（团队长）KPI考核
		else if ("get_group_kpi".equals(apiName)) {
			result = CallApi.callGetGroupKpiApi(param,osFlag);
		}
		
		// 获取该团队财富师列表
		else if ("get_cfs_list".equals(apiName)) {
			result = CallApi.callgetcfsListApi(param,osFlag);
		}
		
		// 财富中心总经理KPI考核
		else if ("get_kernel_kpi".equals(apiName)) {
			result = CallApi.callGetKernelKpiApi(param,osFlag);
		}
		
		// 财富中心团队列表
		else if ("get_group_list".equals(apiName)) {
			result = CallApi.callgetGroupListApi(param,osFlag);
		}
		
		// 业务部总经理KPI考核
		else if ("get_business_kpi".equals(apiName)) {
			result = CallApi.callGetBusinessKpiApi(param,osFlag);
		}
		
		// 业务部财富中心列表
		else if ("get_kenrel_list".equals(apiName)) {
			result = CallApi.callgetKenrelListApi(param,osFlag);
		}
		
		log.info("CallApiServlet result:" + result);
		
		
		// 生成数字签名，加密
		result = ApiCommonUtils.encrypAndSign(result, osFlag);
		out.write(result);
		out.flush();
		out.close();
	}
}
